from apps.pay.order.dao import OrderDao
from django.db.models import Sum
from decimal import Decimal
import datetime
from django.db.models import Q
import json
from apps.common.utils.tools import random_num
from apps.common.utils.time import getFormatTime
from apps.pay.recordIncomes.dao import RecordIncomesDao
from apps.merchant.shop.dao import ShopDao


def shopSettleFun(date):
    print('结算日期', date)
    _itemOrder = OrderDao().findAllSubOrder().filter(Q(orderStatus='WAIT_SEND') | Q(orderStatus='READY_SEND') | Q(orderStatus='WAIT_RECEIVE') | Q(orderStatus='FINISH'), payTime__date=date)
    _recordIncomes = RecordIncomesDao().findAll().filter(createTime__date=date)

    # 加盟店线下店内支付结算
    _shops = ShopDao().findAll().filter(Q(types='0') | Q(types='1'))
    print('加盟店结算')
    for shop in _shops:
        print('店内结算',shop.id)
        moneyLocked = _itemOrder.filter(shop=shop, scene='payForFranchisee').aggregate(sums=Sum('sellingPrice'))['sums'] or 0
        money = _recordIncomes.filter(shop=shop, payway='shop_balance', scene='sendForExpense', changeWay='add').aggregate(sums=Sum('money'))['sums'] or 0
        print('moneyLocked',moneyLocked, money )
        if money > 0:
            receiptAccount = json.dumps({"types":'bank',"paywayCodeUrl":'',"creditedName": shop.creditedName, "creditedBank": shop.creditedBank, "creditedAccount": shop.creditedAccount, "bankCode": shop.bankCode})
            data1 = {}
            data1['scene'] = 'shopinSettle'
            data1['shop'] = shop.id
            data1['receiptAccount'] = receiptAccount
            data1['fromUser'] = ''
            data1['orderId'] = getFormatTime('%Y%m%d%H%M%S') + random_num(8)
            data1['money'] = Decimal(money)
            data1['moneyLocked'] = Decimal(moneyLocked) # moneyLocked字段强行当做订单金额使用
            data1['serviceCharge'] = 0
            data1['realMoney'] = Decimal(money)
            data1['payway'] = 'incomes'
            data1['changeWay'] = 'sub'
            data1['orderStatus'] = 'WAIT_SEND'
            RecordIncomesDao().create(data1)

        print('线上结算',shop.id)
        _itemOrder2 = _itemOrder.filter(Q(scene='generay')|Q(scene='giftSection')|Q(scene='shopItem'))
        moneyLocked = _itemOrder2.filter(Q(shop=shop)).exclude(Q(content__contains='"item":1179') | Q(content__contains='"item": 1179')).aggregate(sums=Sum('sellingPrice'))['sums'] or 0
        money = _itemOrder2.filter(Q(shop=shop)).exclude(Q(content__contains='"item":1179') | Q(content__contains='"item": 1179')).aggregate(sums=Sum('costPrice'))['sums'] or 0
        if money>0:
            receiptAccount = json.dumps({"types":'bank',"paywayCodeUrl":'',"creditedName": shop.creditedName, "creditedBank": shop.creditedBank, "creditedAccount": shop.creditedAccount, "bankCode": shop.bankCode})
            data1 = {}
            data1['scene'] = 'onlineSettle'
            data1['shop'] = shop.id
            data1['receiptAccount'] = receiptAccount
            data1['fromUser'] = ''
            data1['orderId'] = getFormatTime('%Y%m%d%H%M%S') + random_num(8)
            data1['money'] = Decimal(money)
            data1['moneyLocked'] = Decimal(moneyLocked)
            data1['serviceCharge'] = 0
            data1['realMoney'] = Decimal(money)
            data1['payway'] = 'incomes'
            data1['changeWay'] = 'sub'
            data1['orderStatus'] = 'WAIT_SEND'
            RecordIncomesDao().create(data1)